<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of question
 *
 * @author Hubert
 */
class question extends User_Controller{
    function index() {
        $username = $this->session->userdata('username');
        $userid = $this->get_userID($username);
        $user_profile = $this->get_userProfile($userid);
        $data = array(
            'userid' => $userid,
            'username' => $username,
            'email' => $user_profile->email,
            'base_url' => base_url()
        );
        $captcha = $this->get_captcha();
        $this->load->view('user/postQuestion', array('data' => $data, 'captcha' => $captcha));
    }

    function submitQuestion() {
        $username = $this->session->userdata('username');
        $userid = $this->get_userID($username);
        $user_profile = $this->get_userProfile($userid);
        $data = array(
            'userid' => $userid,
            'username' => $username,
            'email' => $user_profile->email,
            'base_url' => base_url()
        );
        $captcha = $this->get_captcha();
        $this->form_validation->set_rules('title', 'title', 'trim|strip_tags|xss_clean|required|max_length[128]');
        $this->form_validation->set_rules('affiliation', 'affiliation', 'trim|strip_tags|xss_clean|required|max_length[64]');
        $this->form_validation->set_rules('category', 'affiliation', 'trim|strip_tags|xss_clean|required');
        $this->form_validation->set_rules('email', 'email', 'trim|strip_tags|xss_clean|required|valid_email');
        $this->form_validation->set_rules('question', 'question', 'trim|strip_tags|xss_clean|required');
        $this->form_validation->set_rules('captcha', 'captcha', 'trim|strip_tags|xss_clean|callback_captcha_check|match_captcha[captcha.word]');
        $this->form_validation->set_error_delimiters('<p class="disclaimer" style="text-align: left; color: red; font-size: 13px">', '</p>');
        if ($this->form_validation->run() == false) {
            // Hasn't run or thera are some validation errors
            $this->session->set_flashdata('error', 'Question failed to post!');
//                redirect('user', array('data' => $data, 'captcha' => $captcha));
            $this->load->view('user/postQuestion', array('data' => $data, 'captcha' => $captcha));
            return;
        } else {
            $username = $this->get_username();
            $title = $this->input->post('title');
            $userid = $this->get_userID($username);
            $affiliation = $this->input->post('affiliation');
            $email = $this->input->post('email');
            $category = $this->input->post('category');
            $question = $this->input->post('question');
            if ($this->database->insertQuestion($userid, $title, $affiliation, $email, $category, $question)) {
                $this->session->set_flashdata('message', 'Question has been posted!');
            } else {
                $this->session->set_flashdata('error', 'Question failed to post!');
            }

            // Everything is go - process the form 
        }
        redirect('user/question', array('data' => $data, 'captcha' => $captcha));
    }
}

?>
